package com.sinog.auth.controller;

import com.sinog.auth.util.CodeUrlUtils;
import com.sinog.auth.util.CookiesUtils;
import com.sinog.auth.util.HttpUtils;
import com.sinog.core.utilbean.Constants;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc 自定义登录页面
 * @author zy
 * @date -
 */
@Log4j2
@Controller
public class LoginController {

    private static final String ERROR = "error";
    private static final String ERROR_MSG = "errormsg";

    /**
     * 页面验证码使用状态
     */
    @Value("${gkzx.azbj.captcha.use.state}")
    private Boolean captchaFrontState;

    /**
     * 这里必须是一个GET请求，POST请求是Spring security 用于提交表单的
     * @param request request
     * @return ModelAndView
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login(HttpServletRequest request) {
        ModelAndView modelAndView;
        // 获取当前省份的localCode
        String localCode = CodeUrlUtils.getLocalCode();
        // 山西个性化合并dev,如果登录省份为山西的话进入山西的登录界面
        if(Constants.XzqhCode.SHAN1XI.equals(localCode)) {
            modelAndView = new ModelAndView("onlogin-sx");
        } else {
            modelAndView = new ModelAndView("onlogin");
        }
        String err = (String)request.getSession().getAttribute("username_err");
        if(!StringUtils.isEmpty(err)) {
            modelAndView.addObject(ERROR_MSG,err);
        } else {
            Map<String,String[]> parameterMap = request.getParameterMap();
            if(parameterMap.containsKey(ERROR)) {
                modelAndView.addObject(ERROR_MSG,"用户名或密码错误");
            }
        }
        modelAndView.addObject("captchaFrontState",captchaFrontState);
        return modelAndView;
    }

    /**
     * 重定向单点登录方式错误页面
     * @param request request
     * @return ModelAndView
     */
    @RequestMapping(value = "/errorMsg", method = RequestMethod.GET)
    public ModelAndView error(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("errorMsg");
        String err = (String)request.getSession().getAttribute("token_parameter");
        if(!StringUtils.isEmpty(err)) {
            modelAndView.addObject(ERROR_MSG,err);
        }else{
            modelAndView.addObject(ERROR_MSG,"手机号为空,");
        }
        return modelAndView;
    }
    @RequestMapping(value = "/errorMsgSx", method = RequestMethod.GET)
    public void errorsx(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String err = (String)request.getSession().getAttribute("code");
        response.sendRedirect("http://app.sft.shanxi.gov.cn/jft/ddlogin/unauth?code="+err);
    }
    /**
     * 接收第三方的请求
     * @param request request
     */
    @RequestMapping(value = "/send", method = RequestMethod.GET)
    @ResponseBody
    public void  send(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //山西单点登录
        String localCode = CodeUrlUtils.getLocalCode();
        Map<String, String> map = new HashMap<>(16);
        if(Constants.XzqhCode.SHAN1XI.equals(localCode)){
            String token = request.getParameter("mobile");
            if(StringUtils.isEmpty(token)){
                //参数为空或者null
                request.getSession().setAttribute("code","500");
                response.sendRedirect("/auth/errorMsgSx");
            }
            map.put("mobile",token);
        } else {
            CookiesUtils.delectCookieByName(request,response);
            String token = request.getParameter("token");
            map.put("token",token);
        }
        HttpUtils.doBgPostReq(response, "/auth/login", map);
    }
}